﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevTeam;

using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Destex
{
    public partial class TopEtiketleme : XForm
    {
        public TopEtiketleme()
        {
            InitializeComponent();
        }

        private void TopEtiketleme_Load(object sender, EventArgs e)
        {
            EnableBarcodeType(0, 10);
            BarcodeRead += PO_BarcodeRead;

            //textBox1.KeyPress += textBox1_KeyPress;
            //this.Shown += TopEtiketleme_Shown;

            gridView1.Init("GetDestexTop");
            gridView1.FormatAsNumber(0, "Qty");
            gridView1.FormatAsDateTime("Tarih");
            gridView1.AddSummary("Qty");
            gridView1.BestFitColumns();
        }

        int ShelfId = 0, RollId = 0;

        void PO_BarcodeRead(int Type, int Barcode)
        {
            if (Type > 0)
            {
                ShelfId = Barcode;
                this.Invoke(new MethodInvoker(() => lblRaf.Text = AppSession.Lookups.Shelf.FindByShelfId(Barcode).Name));
            }
            else
            {
                RollId = Barcode;
                DataTable t = new DataTable();
                this.Invoke(new MethodInvoker(() => t = ExecuteDataTable("UpdDestexTop", RollId, ShelfId)));

                lblArt.Text =
                lblRoll.Text = string.Empty;
                pictureEdit1.Image = null;

                if (t.Rows.Count > 0)
                {
                    lblArt.Text = t.Rows[0]["Product"].ToStr();
                    lblRoll.Text = t.Rows[0]["RollId"].ToStr();

                    if (t.Rows[0]["BLOB"] != DBNull.Value)
                        pictureEdit1.Image = ConvertByteArrayToImage((byte[])t.Rows[0]["BLOB"]);
                    else
                        pictureEdit1.Image = null;

                    DataTable dt = gridView1.DataTable();
                    DataRow dr = dt.NewRow();
                    dr["RollId"] = t.Rows[0]["RollId"];
                    dr["ProductId"] = t.Rows[0]["ProductId"];
                    dr["ShelfId"] = t.Rows[0]["ShelfId"];
                    dr["Qty"] = t.Rows[0]["Qty"];
                    dr["Tarih"] = t.Rows[0]["Tarih"];
                    dt.Rows.Add(dr);

                    gridView1.BestFitColumns();

                }
            }
        }
    }

    /*
    void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if(e.KeyChar == 13)
            {
                barkod = textBox1.Text.Substring(0, textBox1.Text.Length - 1).ToInt();
                if (barkod > 1e9)
                {
                    ShelfId = barkod - 1000000000;
                    lblRaf.Text = AppSession.Lookups.Shelf.FindByShelfId(ShelfId).Name;
                }
                else
                {
                    RollId = barkod;
                    DataTable t = ExecuteDataTable("UpdDestexTop", RollId, ShelfId);
                    lblArt.Text = 
                    lblRoll.Text = string.Empty;
                    pictureEdit1.Image = null;

                    if (t.Rows.Count > 0)
                    {
                        lblArt.Text = t.Rows[0]["Product"].ToStr();
                        lblRoll.Text = t.Rows[0]["RollId"].ToStr();

                        if (t.Rows[0]["BLOB"] != DBNull.Value)
                            pictureEdit1.Image = ConvertByteArrayToImage((byte[])t.Rows[0]["BLOB"]);
                        else
                            pictureEdit1.Image = null;

                        DataTable dt = gridView1.DataTable();
                        DataRow dr = dt.NewRow();
                        dr["RollId"] = t.Rows[0]["RollId"];
                        dr["ProductId"] = t.Rows[0]["ProductId"];
                        dr["ShelfId"] = t.Rows[0]["ShelfId"];
                        dr["Qty"] = t.Rows[0]["Qty"];
                        dr["Tarih"] = t.Rows[0]["Tarih"];
                        dt.Rows.Add(dr);

                        gridView1.BestFitColumns();

                    }
                }
                textBox1.Clear();
            }
    }

    int ShelfId = 0, RollId = 0, barkod = 0;

    void TopEtiketleme_Shown(object sender, EventArgs e)
    {
        textBox1.Focus();
    }
    */
}
